package com.lzdongrui.backend.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lzdongrui.backend.domain.dto.CollegeDTO;
import com.lzdongrui.backend.domain.vo.CampusVO;
import com.lzdongrui.backend.domain.vo.CollegeVO;
import com.lzdongrui.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lzdongrui.common.annotation.Log;
import com.lzdongrui.common.core.controller.BaseController;
import com.lzdongrui.common.core.domain.AjaxResult;
import com.lzdongrui.common.enums.BusinessType;
import com.lzdongrui.backend.domain.College;
import com.lzdongrui.backend.service.ICollegeService;
import com.lzdongrui.common.utils.poi.ExcelUtil;
import com.lzdongrui.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学院Controller
 * 
 * @author wyh
 * @date 2023-04-28
 */
@RestController
@RequestMapping("/backend/college")
public class CollegeController extends BaseController
{
    @Autowired
    private ICollegeService collegeService;

    /**
     * 查询学院列表
     */
    @PreAuthorize("@ss.hasPermi('backend:college:list')")
    @GetMapping("/list")
    public TableDataInfo list(CollegeDTO collegeDTO)
    {
        startPage();
        List<CollegeVO> list = collegeService.selectCollegeList(collegeDTO);
        return getDataTable(list);
    }

    /**
     * 查询学院列表
     */
    @GetMapping("/all-list")
    public TableDataInfo allList(CollegeDTO collegeDTO)
    {
        List<College> list = collegeService.selectCollegeAllList(collegeDTO);
        return getDataTable(list);
    }

    /**
     * 导出学院列表
     */
    @PreAuthorize("@ss.hasPermi('backend:college:export')")
    @Log(title = "学院", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CollegeDTO collegeDTO)
    {
        List<CollegeVO> list = collegeService.selectCollegeList(collegeDTO);
        ExcelUtil<CollegeVO> util = new ExcelUtil<CollegeVO>(CollegeVO.class);
        util.exportExcel(response, list, "学院数据");
    }

    /**
     * 获取学院详细信息
     */
    @PreAuthorize("@ss.hasPermi('backend:college:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(collegeService.selectCollegeById(id));
    }

    /**
     * 新增学院
     */
    @PreAuthorize("@ss.hasPermi('backend:college:add')")
    @Log(title = "学院", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody College college)
    {
        college.setOperator(getUserId());
        return toAjax(collegeService.insertCollege(college));
    }

    /**
     * 修改学院
     */
    @PreAuthorize("@ss.hasPermi('backend:college:edit')")
    @Log(title = "学院", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody College college)
    {
        college.setOperator(getUserId());
        return toAjax(collegeService.updateCollege(college));
    }

    /**
     * 删除学院
     */
    @PreAuthorize("@ss.hasPermi('backend:college:remove')")
    @Log(title = "学院", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(collegeService.deleteCollegeByIds(ids));
    }

    /**
     * 下载模板
     *
     * @param response
     */
    @Anonymous
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CollegeDTO> util = new ExcelUtil<CollegeDTO>(CollegeDTO.class);
        util.importTemplateExcel(response, "学院数据");
    }

    /**
     * 导入学院数据
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "学院", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('backend:college:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        Long Operator = getUserId();
        ExcelUtil<College> util = new ExcelUtil<College>(College.class);
        List<College> colleges = util.importExcel(file.getInputStream());
        String message = collegeService.importCollege(colleges, updateSupport,Operator);
        return success(message);
    }

    /**
     * 查询学院列表(分级查询)
     */
    @GetMapping("/get-list")
    public TableDataInfo getList(College college)
    {
        List<College> list = collegeService.selectCollegeListByCampusId(college);
        return getDataTable(list);
    }
}
